package com.partner.boot.service.impl;

import com.partner.boot.entity.Attractions;
import com.partner.boot.entity.Permission;
import com.partner.boot.mapper.AttractionsMapper;
import com.partner.boot.service.IAttractionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 菜牙买菜
 * @since 2023-07-06
 */
@Service
public class AttractionsServiceImpl extends ServiceImpl<AttractionsMapper, Attractions> implements IAttractionsService {

    @Resource
    private AttractionsMapper attractionsMapper;
    @Override
    public List<Attractions> tree() {
        List<Attractions> list = list();
        return childrenTree(null, list); // 从第一级开始往下递归获取树
    }

    private List<Attractions> childrenTree(Integer pid, List<Attractions> allData) {
        ArrayList<Attractions> list = new ArrayList<>();
        for (Attractions attractions : allData) {
            if (Objects.equals(attractions.getPid(), pid)) {
                list.add(attractions);
                List<Attractions> childrenTree = childrenTree(attractions.getId(), allData);
                attractions.setChildren(childrenTree);
            }
        }
        return list.stream().sorted(Comparator.comparing(Attractions::getSort).reversed()).collect(Collectors.toList());
    }
}
